//
//  MoiveDramaDeriesView.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/8/30.
//

#import "MoiveDramaDeriesView.h"
#import "RVideoObj.h"

@interface MoiveDramaDeriesView ()

@property(nonatomic,strong)MyLinearLayout* titleLayout;

/// title
@property(nonatomic,strong)UILabel* titleLb;

/// 缓存
@property(nonatomic,strong)UIButton* cacheBtn;

/// 选集
@property(nonatomic,strong)UIButton* choiseBtn;

@property(nonatomic,strong)UIScrollView *scrollView;

/// 集数
@property(nonatomic,strong)MyLinearLayout* flowLayout;

/// btn
@property(nonatomic,strong)NSArray<UIButton*>* btns;

@end

@implementation MoiveDramaDeriesView

- (instancetype)init{
    if(self = [super initWithOrientation:(MyOrientation_Vert)]){
        
        self.myHeight = MyLayoutSize.wrap;
        self.backgroundColor = UIColor.whiteColor;
        self.padding = UIEdgeInsetsMake(16, 0, 16, 0);
        self.subviewVSpace = 10;
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews{
    MyRelativeLayout* titleLayout = [MyRelativeLayout new];
    titleLayout.myMargin = 0;
    titleLayout.myHeight = MyLayoutSize.wrap;
    titleLayout.padding = UIEdgeInsetsMake(0, 16, 0, 16);
    [self addSubview:titleLayout];
    
    UILabel* titleLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@""];
    titleLb.myCenterY = titleLb.myLeft = 0;
    [titleLayout addSubview:titleLb];
    _titleLb = titleLb;
    
    UIButton* choiseBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [choiseBtn setTitle:@"选集" forState:UIControlStateNormal];
    [choiseBtn setTitleColor:COLOR_BLUE forState:UIControlStateNormal];
    choiseBtn.titleLabel.font = FONT_FIT(12);
    [choiseBtn addTarget:self action:@selector(choisePlayClicked:) forControlEvents:UIControlEventTouchUpInside];
    choiseBtn.myRight = choiseBtn.myCenterY = 0;
    choiseBtn.mySize = CGSizeMake(PX(50), PX(22));
    choiseBtn.myVertMargin = 0;
    choiseBtn.layer.cornerRadius = PX(11);
    choiseBtn.layer.masksToBounds = YES;
    choiseBtn.layer.borderColor = COLOR_BLUE.CGColor;
    choiseBtn.layer.borderWidth = Size_1PX;
    [titleLayout addSubview:choiseBtn];
    _choiseBtn = choiseBtn;
    
    UIButton* cacheBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [cacheBtn setTitle:@"缓存" forState:UIControlStateNormal];
    [cacheBtn setTitleColor:COLOR_BLUE forState:UIControlStateNormal];
    cacheBtn.titleLabel.font = FONT_FIT(12);
    [cacheBtn addTarget:self action:@selector(choiseCacheClicked:) forControlEvents:UIControlEventTouchUpInside];
    cacheBtn.rightPos.equalTo(choiseBtn.leftPos).offset(10);
    cacheBtn.myCenterY = 0;
    cacheBtn.mySize = CGSizeMake(PX(50), PX(22));
    cacheBtn.layer.cornerRadius = PX(11);
    cacheBtn.layer.masksToBounds = YES;
    cacheBtn.layer.borderColor = COLOR_BLUE.CGColor;
    cacheBtn.layer.borderWidth = Size_1PX;
    [titleLayout addSubview:cacheBtn];
    _cacheBtn = cacheBtn;
    
    //底部滚动控件
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.myMargin = 0;
    scrollView.myHeight = MyLayoutSize.wrap;
    [self addSubview:scrollView];
    _scrollView = scrollView;
    
    // 流式布局
    MyLinearLayout *flowLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    flowLayout.subviewSpace = 10;
    flowLayout.padding = UIEdgeInsetsMake(0, 16, 0, 16);
    flowLayout.myLeft = 0;
    flowLayout.myVertMargin = 0;
    flowLayout.myHeight = MyLayoutSize.wrap;
    flowLayout.myWidth = MyLayoutSize.wrap;
    [scrollView addSubview:flowLayout];
    _flowLayout = flowLayout;
}


- (void)setUrlGroup:(RVideoUrlGroup *)urlGroup{
    _urlGroup = urlGroup;
    
    self.titleLb.text = [NSString stringWithFormat:@"%@(共%ld集)",urlGroup.title,urlGroup.urls.count];
    [self.titleLb sizeToFit];
    
    NSMutableArray* btnsArr = [NSMutableArray array];
    
    for (int i = 0 ; i < urlGroup.urls.count; i++) {
        
        RVideoUrlObj* urlObj = urlGroup.urls[i];
        urlObj.index = i;
        
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = COLOR_BACKGEOUND;
        btn.myHeight = PX(36);
        btn.widthSize.equalTo(@(MyLayoutSize.wrap)).min(80).add(32);
        [btn setTitleColor:HEXCOLOR(0x666666) forState:UIControlStateNormal];
        [btn setTitleColor:COLOR_THEME forState:UIControlStateSelected];
        btn.titleLabel.font = FONT_FIT(12);
        [btn addTarget:self action:@selector(playClicked:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:urlObj.url_info forState:UIControlStateNormal];
        btn.layer.cornerRadius = 2;
        
        btn.tag = 100+i;
        [btn sizeToFit];
        [self.flowLayout addSubview:btn];
        [btnsArr addObject:btn];
    }
    self.btns = btnsArr;
}

/// 选择了第几集
/// @param index -
- (void)playAt:(NSInteger)index{
    for (UIButton* btn in self.btns) {
        btn.selected = NO;
    }
    
    CGFloat x = 0;
    if(index >= 0 && index < self.btns.count){
        UIButton* btn = self.btns[index];
        btn.selected = YES;
        self.titleLb.textColor = COLOR_THEME;
        x = [self.flowLayout subview:btn estimatedRectInLayoutSize:CGSizeZero].origin.x;
    }else{
        self.titleLb.textColor = COLOR_BLACK_DARK;
    }
    
    [self.scrollView scrollRectToVisible:(CGRectMake(x, 0, 100, 40)) animated:YES];
}

#pragma mark  -- action

//播放
- (void)playClicked:(UIButton*)sender{
    for (UIButton* btn in self.btns) {
        btn.selected = NO;
    }
    sender.selected = !sender.selected;
    self.titleLb.textColor = COLOR_ORANGE;
    
    NSInteger index = sender.tag - 100;
    RVideoUrlObj* urlObj = self.urlGroup.urls[index];
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(playUrl:atIndexPath:)]){
        [self.delegate playUrl:urlObj.url_m3u8 atIndexPath:[NSIndexPath indexPathForRow:urlObj.index inSection:self.urlGroup.index]];
    }
}

//选择
- (void)choisePlayClicked:(UIButton*)sender{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(choiseDramaForPurpose:atIndex:)]){
        [self.delegate choiseDramaForPurpose:1 atIndex:self.urlGroup.index];
    }
}

//缓存
- (void)choiseCacheClicked:(UIButton*)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(choiseDramaForPurpose:atIndex:)]){
        [self.delegate choiseDramaForPurpose:2 atIndex:self.urlGroup.index];
    }
}
@end
